# _*_ coding: utf-8 _*_
import queue
import time
import serial

import gps
import opQue


class comThread:
    # 构造串口属性
    def __init__(self, Port = 'com5', Baudrate = 115200):
        self.l_serial = None
        self.alive = False
        self.port = Port # 串口名称
        self.data = None
        self.baudrate = Baudrate # 波特率

    # 串口启动函数
    def start(self):
        self.l_serial = serial.Serial()
        self.l_serial.port = self.port
        self.l_serial.baudrate = self.baudrate
        # 设置等待时间
        self.l_serial.timeout = 2
        self.l_serial.open()
        # 判断串口是否打开
        if self.l_serial.isOpen():
            self.alive = True
            print('%s open' % self.port)
            return True
        else:
            self.alive = False
            print('Port open error')
            return False

    # 串口读取函数
    def comRead(self, queue):
        data = ''
        data = data.encode('utf-8')
        while True:
            time.sleep(0.01)
            n = self.l_serial.inWaiting()
            if n:
                data += self.l_serial.read(n)
                # print(type(data))

            n = self.l_serial.inWaiting()
            if len(data) > 0 and n == 0:
                try:
                    temp = data.decode('utf-8')
                    data = ''
                    data = data.encode('utf-8')
                    print(temp)
                    # 将数据放入队列
                    opQue.writerQ(queue=queue, data=temp)
                except:
                    print("data error, try again\n")
                    break

    # 串口关闭函数
    def stop(self):
        self.alive = False
        if self.l_serial.isOpen():
            self.l_serial.close()


def main(queue, Port = 'com5', Baudrate = 115200,):
    a = comThread(Port=Port, Baudrate=Baudrate)   # 定义一个串口类
    a.start()  # 启动串口属性
    a.comRead(queue=queue) # 启动接收程序
    a.stop()    # 关闭程序


def comRead(queue):
    comThread = serial.Serial(
        port='com5', baudrate=115200,
        timeout=2
    )
    if comThread.isOpen() is not True:
        comThread.open()
    if comThread.isOpen():
        print(comThread.port)
        data = ''
        data = data.encode('utf-8')
        while True:
            time.sleep(0.01)
            n = comThread.inWaiting()
            if n:
                data += comThread.read(n)
                # print(type(data))

            n = comThread.inWaiting()
            if len(data) > 0 and n == 0:
                try:
                    temp = data.decode('utf-8')
                    data = ''
                    data = data.encode('utf-8')
                    print(temp)
                    # 将数据放入队列
                    opQue.writerQ(queue, temp)

                except:
                    print(data, len(data))
                    data = ''
                    data = data.encode('utf-8')
                    # print("data error, try again\n")
                    # break

        comThread.close()
    pass


if __name__ == '__main__':
    q = queue.Queue()
    main(q)







